import request from '@/utils/request'
import type {
  UserResponseData,
  User,
  AllRoleResponseData,
  assignRolesData,
} from '@/api/acl/user/type'

enum API {
  // 用户分页
  ALL_USER_URL = '/admin/acl/user',
  // 添加用户
  ADD_USER_URL = '/admin/acl/user/save',
  // 更新用户
  UPDATE_USER_URL = '/admin/acl/user/update',
  // 所有职位
  ALL_RULE_URL = '/admin/acl/user/toAssign',
  // 分配角色
  ASSIGN_ROLE_URL = '/admin/acl/user/doAssignRole',
  // 删除某一个用户
  DELETE_USER_URL = '/admin/acl/user/remove',
  // 批量删除用户
  BATCH_DELETE_USER_URL = '/admin/acl/user/batchRemove',
}

export const reqUserInfo = (page: number, limit: number, username: string) =>
  request.get<any, UserResponseData>(
    `${API.ALL_USER_URL}/${page}/${limit}?username=${username}`,
  )

// 添加更新用户接口
export const reqAddOrUpdateUser = (data: User) => {
  if (data.id) {
    return request.put<any, any>(API.UPDATE_USER_URL, data)
  } else {
    return request.post<any, any>(API.ADD_USER_URL, data)
  }
}

export const reqAllRole = (adminId: number) =>
  request.get<any, AllRoleResponseData>(`${API.ALL_RULE_URL}/${adminId}`)

export const reqAssignRole = (data: assignRolesData) =>
  request.post<any, any>(API.ASSIGN_ROLE_URL, data)

export const reqDeleteUser = (userId: number) =>
  request.delete<any, any>(`${API.DELETE_USER_URL}/${userId}`)

export const reqBatchDeleteUser = (idList: number[]) =>
  request.delete<any, any>(API.BATCH_DELETE_USER_URL, { data: idList })
